package cn.zn.base.common.utils;

import cn.hutool.http.HttpStatus;
import cn.zn.base.bean.dto.ResultData;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResultUtil {

    /**
     * 返回成功
     *
     * @return 结果
     */
    public static <T> ResultData<T> success() {
        return success("请求成功");
    }

    /**
     * 返回成功
     *
     * @param message 消息
     * @return 结果
     */
    public static <T> ResultData<T> success(String message) {
        return success(message, null);
    }

    /**
     * 返回成功
     *
     * @param data 消息
     * @return 结果
     */
    public static <T> ResultData<T> success(T data) {
        return success("请求成功", data);
    }

    /**
     * 返回成功
     *
     * @param message 消息
     * @param data    数据
     * @return 结果
     */
    public static <T> ResultData<T> success(String message, T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(HttpStatus.HTTP_OK);
        resultData.setMsg(message);
        resultData.setData(data);
        return resultData;
    }

    /**
     * 返回异常
     *
     * @return 结果
     */
    public static <T> ResultData<T> error() {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, "请求异常", null);
    }

    /**
     * 返回异常
     *
     * @param message 消息
     * @return 结果
     */
    public static <T> ResultData<T> error(String message) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, message, null);
    }

    /**
     * 返回异常
     *
     * @param message 消息
     * @return 结果
     */
    public static <T> ResultData<T> error(String message, T data) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR, message, data);
    }

    /**
     * 返回异常
     *
     * @param message 消息
     * @return 结果
     */
    public static <T> ResultData<T> error(Integer code, String message) {
        return error(code, message, null);
    }

    /**
     * 返回异常
     *
     * @param code    返回code
     * @param message 消息
     * @return 结果
     */
    public static <T> ResultData<T> error(Integer code, String message, T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMsg(message);
        resultData.setData(data);
        return resultData;
    }

    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string   待渲染的字符串
     * @return null
     */
    public static void renderString(HttpServletResponse response, String string) {
        PrintWriter printWriter = null;
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            printWriter = response.getWriter();
            printWriter.print(string);
            printWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
    }

}
